package com.tvsinfo.jdk.demo.case4_stream;

import com.tvsinfo.jdk.demo.case2_function_interface.dto.Car;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

/**
 * 分组、分区
 */
public class Stream3_group_partitioning implements Stream_Create_List {


    public static void main(String[] args) {
        new Stream3_group_partitioning().groupBy();


    }

    /**
     * 四、集合器-分组
     */
    public void groupBy() {
        List<Car> cars = getList();

        System.out.println("分组-1--------------------------------");
        //1.分组-1
        Map<Integer, List<Car>> listMap = cars.stream().collect(Collectors.groupingBy(car -> car.getPrice()));
        listMap.forEach((integer, carList) -> System.out.println("key:" + integer + " value:" + carList));


        //2.分组-2（类似多级分组）
        System.out.println("分组-2--------------------------------");
        Map<Integer, Map<String, List<Car>>> collect = cars.stream().collect(Collectors.groupingBy(Car::getPrice, Collectors.toMap(Car::getBrand, Arrays::asList)));
        collect.forEach((integer, carList) -> System.out.println("key:" + integer + " value:" + carList));

        //3.分组-3
        System.out.println("分组-3--------------------------------");
        Map<Integer, List<Car>> integerMapMap = cars.stream().collect(Collectors.groupingBy(Car::getPrice, TreeMap::new, Collectors.toList()));
        integerMapMap.forEach((integer, carList) -> System.out.println("key:" + integer + " value:" + carList));


        //4.实战多级分组
        System.out.println("分组-4--------------------------------");
        Map<Integer, Map<String, List<Car>>> collect4 = cars.stream().collect(Collectors.groupingBy(Car::getPrice, Collectors.groupingBy(Car::getBrand, Collectors.toList())));
        collect4.forEach((integer, carList) -> System.out.println("key:【" + integer + "】 value:" + carList));


        //
        System.out.println("分组-5（自定义）--------------------------------");
        Map<Integer, Car> integerCarMap = cars.stream().collect(Collectors.groupingBy(Car::getId, Collectors.collectingAndThen(Collectors.toList(), cars1 -> {
            return cars1.get(0);
        })));
        integerCarMap.forEach((integer, carList) -> System.out.println("key:【" + integer + "】 value:" + carList));

    }


    /**
     * 五、集合器-分区
     */
    public void partitioningBy() {
        List<Car> cars = getList();

        //默认分区
        System.out.println("分区-1--------------------------------");
        Map<Boolean, List<Car>> collect = cars.stream().collect(Collectors.partitioningBy(car -> car.getPrice() > 30));
        collect.forEach((integer, carList) -> System.out.println("key:【" + integer + "】 value:" + carList));

        System.out.println("分区-2--------------------------------");
        Map<Boolean, Map<Integer, List<Car>>> collect2 = cars.stream().collect(Collectors.partitioningBy(car -> car.getPrice() > 30, Collectors.groupingBy(Car::getPrice, Collectors.toList())));
        collect2.forEach((integer, carList) -> System.out.println("key:【" + integer + "】 value:" + carList));
    }

}
